package com.example.smart2.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smart2.R;
import com.example.smart2.model.PressModel;
import com.example.smart2.ui.PressDetailsActivity;

public class PressAdapter extends RecyclerView.Adapter<PressAdapter.ViewHolder> {
    private Context context;
    private PressModel pressModel;

    public PressAdapter(Context context,PressModel pressModel){
        this.context=context;
        this.pressModel=pressModel;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_press,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PressModel.RowsBean rowsBean=pressModel.getRows().get(position);
        String name=rowsBean.getTitle();
        String url=rowsBean.getCover()+"";
        String str="http://124.93.196.45:10001";
        holder.title.setText(name);
        if(url.equals("local")){
            Glide.with(context)
                    .load(rowsBean.getRemark())
                    .placeholder(R.drawable.slide1)
                    .apply(new RequestOptions().transform(new RoundedCorners(20)))
                    .into(holder.imageView);
        } else{
            Glide.with(context)
                    .load(str+url)
                    .placeholder(R.drawable.slide1)
                    .apply(new RequestOptions().transform(new RoundedCorners(20)))
                    .into(holder.imageView);
        }

        String contentStr=rowsBean.getContent()
                .replaceAll("<[.[^<]]*>","")
                .replaceAll(" ","")
                .replaceAll("&nbsp","");
        holder.content.setText(contentStr);
        holder.date.setText(rowsBean.getPublishDate());
        holder.commentNum.setText(rowsBean.getCommentNum()+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, PressDetailsActivity.class);
//                intent.putExtra("data",rowsBean);
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return pressModel.getRows().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView title;
        private TextView content;
        private TextView date;
        private TextView commentNum;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=(ImageView) itemView.findViewById(R.id.imageView);
            title=(TextView) itemView.findViewById(R.id.title);
            content=(TextView) itemView.findViewById(R.id.content);
            date=(TextView) itemView.findViewById(R.id.date);
            commentNum=(TextView) itemView.findViewById(R.id.commentNum);
        }
    }
}
